package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @ClassName $ {NAME}
 * @Description 未完成修改功能
 * @Author 寇铭
 * @Date 2022/6/13 11:19
 * @Version 1.0
 **/
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;


    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息，操作setmeal，执行insert操作
        this.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes.stream().map(item -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //保存套餐和菜品的关联信息，操作setmeal_dish,执行insert操作
        setmealDishService.saveBatch(setmealDishes);

    }

    /**
     * 删除套餐，同时需要删除套餐和菜品的关联数据
     * @param ids
     */
    @Transactional
    @Override
    public void removeWithDish(List<Long> ids) {
        //select count(*) from setmeal where id in (1,2,3) and status = 1
        //查询套餐状态，确定是否可用删除
        LambdaQueryWrapper<Setmeal> sQueryWrapper = new LambdaQueryWrapper<>();
        sQueryWrapper.in(Setmeal::getId, ids);
        sQueryWrapper.eq(Setmeal::getStatus, 1);//1为启售中

        int count = this.count(sQueryWrapper);
        if (count > 0) {
            //如果不能删除，抛出一个业务异常
            throw new CustomException("套餐正在售卖中，不能删除");
        }

        //如果可以删除，先删除套餐表中的数据---setmeal
        this.removeByIds(ids);//id必须是主键值

        //delete from setmeal_dish where setmeal_id in (1,2,3)
        LambdaQueryWrapper<SetmealDish> sdQueryWrapper = new LambdaQueryWrapper<>();
        sdQueryWrapper.in(SetmealDish::getSetmealId, ids);
        //删除关系表中的数据-----setmeal_dish
        setmealDishService.remove(sdQueryWrapper);

    }

    /*
    修改套餐的状态
     */
    @Override
    public void updateStatusByIds(int status, Long[] ids) {

        List<Long> setmealList = new ArrayList<>();
        for (Long id : ids) {
            setmealList.add(id);
        }

        Setmeal setmeal = new Setmeal();
        if (status == 0) {
            for (Long id : setmealList) {
                setmeal.setId(id);
                setmeal.setStatus(0);
                this.updateById(setmeal);
            }

        }else {
            for (Long id : setmealList) {
                setmeal.setId(id);
                setmeal.setStatus(1);
                this.updateById(setmeal);
            }
        }


    }

    @Override
    public void updateSetmeal(SetmealDto setmealDto) {
        //修改套餐信息
        this.updateById(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();//修改后的菜品信息
        Long setmealId = setmealDto.getId();//套餐id
        //删除之前套餐的菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealId);
        setmealDishService.remove(queryWrapper);
        //给每一个菜品设置套餐id
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        //将菜品信息保存到套餐菜品表
        setmealDishService.saveBatch(setmealDishes);
    }


}


















